package com.eastcom.providor.dao.impl;

import java.util.List;
import java.util.Map;

import org.hibernate.Query;
import org.hibernate.Session;

import com.eastcom.bm.atom.Atom_MetalKpiBase;
import com.eastcom.bm.atom.Atom_MetalKpiExpand;
import com.eastcom.providor.dao.BaseDao;
import com.eastcom.providor.dao.IMetalKpiExpandDao;
import com.eastcom.providor.service.extview.MetalKpiExpandUtil;

public class MetalKpiExpandDao extends BaseDao implements IMetalKpiExpandDao {

	public void editMetalKpiExpands(List<Atom_MetalKpiExpand> metalKpiExpands)throws Exception {

	}

	public void editMetalKpiExpandByKpiId(List<Atom_MetalKpiExpand> metalKpiExpands, String kpiId)throws Exception {

	}

	public void editLstVersionMetalKpiExpandByKpiId(String kpiId, Atom_MetalKpiExpand metalKpiExpand) throws Exception {
		if(metalKpiExpand==null||kpiId==null)
			return;
		
		try{
			Atom_MetalKpiBase kpiBaseDefine=new Atom_MetalKpiBase();
			kpiBaseDefine.setId(kpiId);
			metalKpiExpand.setKpiBaseDefine(kpiBaseDefine);
			this.saveObject(metalKpiExpand);
		}
		catch(Exception e){
			throw e;
		}
		
	}

	public void saveMetalKpiExpands(List<Atom_MetalKpiExpand> metalKpiExpands)throws Exception {
		if(metalKpiExpands==null||metalKpiExpands.isEmpty())
			return;
		
		try{
			this.saveObjects(metalKpiExpands);
		}
		catch(Exception e){
			throw e;
		}
	}

	public List<Atom_MetalKpiExpand> selectMetalKpiExpandByKpiId(String kpiId,boolean initialKpiBase)throws Exception {
		Session session=null;
		
		try{
			session=this.getSession();
			Query query=session.createQuery("from Atom_MetalKpiExpand where kpiBaseDefine.id='"+kpiId+"' order by version");
			query.setCacheable(true);
			List<Atom_MetalKpiExpand> metalKpiExpands= query.list();
			if(metalKpiExpands!=null&&initialKpiBase){
				for(Atom_MetalKpiExpand metalKpiExpand:metalKpiExpands)
					metalKpiExpand.getKpiBaseDefine();
			}
			else if(!initialKpiBase)
				metalKpiExpands=MetalKpiExpandUtil.createDatasetDefine(metalKpiExpands);
				
			return metalKpiExpands;
		}
		catch(Exception e){
			throw e;
		}
		finally{
			this.releaseSession(session);
		}
	}

	public Map<Object,Map> selectKpiLastVersion() throws Exception {
		try{
            StringBuffer sql=new StringBuffer("select kpi_id,max(version) as version from QA_METAL_KPI_EX_DEFINE ");
            sql.append("group by kpi_id");
            
            return this.getFieldMapWithKeyFildBySQL(sql.toString(), "kpi_id", new String[]{"version"});
		}
		catch(Exception e){
			throw e;
		}

	}

	public Map<Object, Map> selectKpiLastVersionByKpiId(String kpiId) throws Exception {
		try{
            StringBuffer sql=new StringBuffer("select kpi_id,max(version) as version from QA_METAL_KPI_EX_DEFINE ");
            sql.append("where kpi_id='"+kpiId+"' group by kpi_id");
            
            return this.getFieldMapWithKeyFildBySQL(sql.toString(), "kpi_id", new String[]{"version"});
		}
		catch(Exception e){
			throw e;
		}
	}

	public Map<Object,Map> selectMetalKpiExpOfLastVersion() throws Exception {
		try{
			String sql="select e1.* from QA_METAL_KPI_EX_DEFINE e1 " +
					"where (e1.KPI_ID,e1.version) in"+
			        "(select e2.kpi_id,max(e2.version) as version " +
			        "from QA_METAL_KPI_EX_DEFINE e2 group by e2.kpi_id)";
			
			return this.getFieldMapWithKeyFildBySQL(sql, "kpi_id", new String[]{"version","granularity","dimension","source_desc","arithmetic","delay","collect_desc","update_name"});
		}
		catch(Exception e){
			throw e;
		}
	}

}
